const CompressionPlugin = require('compression-webpack-plugin');
const cdn = {
    dev:{
        css:[
            'https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.css',
            'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.1/theme-chalk/index.min.css'
        ],
        js:[]
    },
    build:{
        css:[
            'https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.css',
            'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.1/theme-chalk/index.min.css'
        ],
        js:[
            'https://cdn.bootcdn.net/ajax/libs/nprogress/0.2.0/nprogress.js',
            'https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.min.js',
            'https://cdn.bootcdn.net/ajax/libs/vue-router/3.5.1/vue-router.min.js',
            'https://cdn.bootcdn.net/ajax/libs/vuex/3.6.2/vuex.min.js',
            'https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js',
            'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.1/index.min.js'
        ]
    }
}

module.exports = {
    publicPath:'/',
    productionSourceMap:false, //打包不生成map映射文件，默认是true
    devServer:{
        proxy:{
            '^/api':{
                target:'http://82.156.49.235:7005',
                pathRewrite:{
                    '^/api':''
                }
            }
        }
    },
    configureWebpack: config => {
        //区分线上环境和开发环境
        if (process.env.NODE_ENV === 'production') {
          // 为生产环境修改配置...
            config.externals = {
                'nprogress':'NProgress',
                'vue':'Vue',
                'vue-router':'VueRouter',
                'vuex':'Vuex',
                'axios':'axios',
                'element-ui':'ELEMENT'
            }
            //去除console
            config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
        } else {
          // 为开发环境修改配置...
        }
      },
    chainWebpack: config => {
        if (process.env.NODE_ENV === 'production') {
            config.plugin('compressionPlugin')
            .use(new CompressionPlugin({
                filename: '[path].gz[query]',
                algorithm: 'gzip',
                test: /\.(js|css)(\?.*)?$/i,
                threshold: 10240, // 对超过10k的数据进行压缩
                minRatio: 0.8, // 只有压缩率小于这个值的资源才会被处理
                deleteOriginalAssets: true, // 删除原文件
            }));
        }
        config
            .plugin('html')
            .tap(args => {
                if(process.env.NODE_ENV === 'production'){
                    //线上环境
                    args[0].css = cdn.build.css;

                    args[0].js = cdn.build.js;

                }else{
                    args[0].css = cdn.dev.css;

                    args[0].js = cdn.dev.js;
                }
                return args
            })
    }
}
